package com.foreknow.config;

import com.foreknow.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 跨域请求支持/token拦截
 * tip:只能写在一个配置类里
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    private TokenInterceptor tokenInterceptor;

    //构造方法
    public WebConfiguration(TokenInterceptor tokenInterceptor) {
        this.tokenInterceptor = tokenInterceptor;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOrigins("*");
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        //排除拦截
        excludePath.add("/user/register");  //登录
        excludePath.add("/user/**");     //注册
        excludePath.add("/superadmin/listarea");//排除拦截
        excludePath.add("/static/**");  //静态资源
        excludePath.add("/assets/**");  //静态资源
        excludePath.add("/hissytemcontroller/selectHis");//侧边栏
        excludePath.add("/departmentController/**");//科室管理
        excludePath.add("/diseCategoryController/**");//诊断目录管理
        excludePath.add("/diseaseController/**");//诊断目录管理2
        excludePath.add("/constanttypecontroller/**");
        excludePath.add("/constantitemcontroller/**");
        excludePath.add("/RegisteLevelController/**");
        excludePath.add("/SettleCategoryController/**");
//        excludePath.add("/user/his_user");     //用户管理
//        excludePath.add("/user/selectBySS");     //根据条件查询用户名
//        excludePath.add("/user/addUser");     //添加用户
//        excludePath.add("/user/showEditUser");     //修改界面绑定当前用户
//        excludePath.add("/user/editUser");     //修改用户
//        excludePath.add("/user/removeUser");     //修改用户
        excludePath.add("/department/departmentList");     //查询所有科室
//        excludePath.add("/constanttypecontroller/getconstanttypelist");     //查询所有常数项（职称用）
        excludePath.add("/FmeditemController/allFmeditem");     //查询所有非药品收费项目
        excludePath.add("/ExpenseClassController/expenseclasslist");



        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath)
                // swagger-ui
                .excludePathPatterns("/swagger-resources/**", "/webjars/**",
                        "/v2/**", "/swagger-ui.html/**");

        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
